<?php

namespace app\admin\controller;

use thinkct\controller\Backend;
use thinkct\facade\Sys;
use think\facade\Cache;
use think\facade\Config;
use app\admin\model\Config as ConfigModel;

class System extends Backend
{
    public function index(): void
    {
        $query = Cache::get('sys_query')['data'] ?? [];
        $query['update_date'] = get_sys_config('system_update_date') ?? '';
        $this->success('success', [
            'query' => $query,
            'version' => Config::get('version')
        ]);
    }

    public function update(): void
    {
        $is_update = $this->request->post('is_update', 0);
        $update = Sys::update($is_update);
        if ($update['code'] === 1) {

            if($is_update == 1) {
                ConfigModel::where('name', 'system_update_date')->update(['value' => date('Y-m-d')]);
                Cache::delete('sys_query');
            }
            Cache::set('sys_query_new_version', $update['data']['version'] ?? '');
            $this->success($update['msg'], $update['data']);
        } else {
            $this->error($update['msg'], $update['data']);
        }
    }

    public function update_log()
    {
        $update_log = Sys::update_log();
        $this->success($update_log['msg'], $update_log['data']);
    }
}